package com.baizhi.action;

import com.baizhi.entity.Celebrity;
import com.baizhi.service.CelebrityService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@Scope("prototype")
public class CelebrityAction {
    @Autowired
    private CelebrityService celebrityService;
    private HttpServletResponse response = ServletActionContext.getResponse();
    private Map<String, Object> map = new HashMap<>();
    private ObjectMapper mapper = new ObjectMapper();

    private Integer pageNum;
    private Integer pageSize;
    private Integer id;
    private Celebrity celebrity;

    public CelebrityAction(){
    }

    //展示所有玩家信息
    public void show() throws IOException {
        List<Celebrity> celebrities = celebrityService.selectCelebrities(pageNum, pageSize);
        PageInfo<Celebrity> pageInfo = new PageInfo<>(celebrities,3);
        int[] pages = pageInfo.getNavigatepageNums();
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        map.put("celebrities", celebrities);
        map.put("pages", pages);
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    //删除玩家信息
    public void deleteCelebrity() throws IOException {
        try{
            celebrityService.deleteCelebrityById(id);
            map.put("status", "success");
        }catch (Exception e){
            e.printStackTrace();
            map.put("status", "failed");
        }
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    //添加玩家信息
    public void addCelebrity() throws IOException {
        celebrityService.insertCelebrity(celebrity);
        map.put("status", "success");
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    public void showOne() throws IOException {
        Celebrity celebrity = celebrityService.selectCelebrityById(id);
        map.put("celebrity", celebrity);
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }
    public void updateCelebrity() throws IOException {
        celebrityService.updateCelebrity(celebrity);
        map.put("status", "success");
        response.setCharacterEncoding("utf-8");
        PrintWriter pw = response.getWriter();
        String s = mapper.writeValueAsString(map);
        pw.print(s);
        pw.flush();
    }



    public Integer getPageNum() {
        return pageNum;
    }
    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }
    public Integer getPageSize() {
        return pageSize;
    }
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Celebrity getCelebrity() {
        return celebrity;
    }
    public void setCelebrity(Celebrity celebrity) {
        this.celebrity = celebrity;
    }
    public void setCelebrityService(CelebrityService celebrityService) {
        this.celebrityService = celebrityService;
    }
}
